package com.ruoyi.web.controller.admin;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.dto.AfterSaleMaintenanceAddDTO;
import com.ruoyi.system.dto.AfterSaleMaintenanceQueryDTO;
import com.ruoyi.system.dto.AfterSaleMaintenanceUpdateDTO;
import com.ruoyi.system.service.IAfterSaleMaintenanceService;
import com.ruoyi.system.vo.AfterSaleMaintenanceVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 售后维护管理Controller
 * 
 * @author ruoyi
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/admin/afterSaleMaintenance")
public class AfterSaleMaintenanceController extends BaseController {

    @Resource
    private IAfterSaleMaintenanceService afterSaleMaintenanceService;

    /**
     * 查询售后维护列表
     */
//    @PreAuthorize("@ss.hasPermi('system:afterSaleMaintenance:list')")
    @GetMapping("/list")
    public TableDataInfo list(AfterSaleMaintenanceQueryDTO queryDTO) {
        startPage();
        List<AfterSaleMaintenanceVO> list = afterSaleMaintenanceService.selectAfterSaleMaintenanceList(
                queryDTO, 1, Integer.MAX_VALUE);
        return getDataTable(list);
    }

    /**
     * 获取售后维护详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:afterSaleMaintenance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        AfterSaleMaintenanceVO vo = afterSaleMaintenanceService.selectAfterSaleMaintenanceById(id);
        return success(vo);
    }

    /**
     * 新增售后维护
     */
//    @PreAuthorize("@ss.hasPermi('system:afterSaleMaintenance:add')")
    @Log(title = "售后维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody AfterSaleMaintenanceAddDTO addDTO) {
        boolean result = afterSaleMaintenanceService.insertAfterSaleMaintenance(addDTO);
        return result ? success() : error("新增失败");
    }

    /**
     * 修改售后维护
     */
//    @PreAuthorize("@ss.hasPermi('system:afterSaleMaintenance:edit')")
    @Log(title = "售后维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody AfterSaleMaintenanceUpdateDTO updateDTO) {
        boolean result = afterSaleMaintenanceService.updateAfterSaleMaintenance(updateDTO);
        return result ? success() : error("修改失败");
    }

    /**
     * 删除售后维护
     */
//    @PreAuthorize("@ss.hasPermi('system:afterSaleMaintenance:remove')")
    @Log(title = "售后维护", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        boolean result = afterSaleMaintenanceService.deleteAfterSaleMaintenanceByIds(ids);
        return result ? success() : error("删除失败");
    }

    /**
     * 获取紧急程度选项
     */
    @GetMapping("/urgencyLevelOptions")
    public AjaxResult getUrgencyLevelOptions() {
        List<Map<String, Object>> options = new ArrayList<>();
        options.add(createOption(1, "低"));
        options.add(createOption(2, "中"));
        options.add(createOption(3, "高"));
        options.add(createOption(4, "紧急"));
        return success(options);
    }

    /**
     * 获取状态选项
     */
    @GetMapping("/statusOptions")
    public AjaxResult getStatusOptions() {
        List<Map<String, Object>> options = new ArrayList<>();
        options.add(createOption(1, "待处理"));
        options.add(createOption(2, "处理中"));
        options.add(createOption(3, "已完成"));
        options.add(createOption(4, "已取消"));
        return success(options);
    }

    /**
     * 创建选项Map
     */
    private Map<String, Object> createOption(Integer value, String label) {
        Map<String, Object> option = new HashMap<>();
        option.put("value", value);
        option.put("label", label);
        return option;
    }
}
